package com.kx.game.center.servlet;

import com.kx.game.center.global.GSServices;
import com.kx.game.center.local.response.LocalErrorResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 推送好友(地区推送 随机推送)各4位
 */
@WebServlet(urlPatterns = "/newFriends")
public class NewFriendsServlet extends HttpServlet {

    private static Logger logger = LoggerFactory.getLogger(LogoutServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String userId = request.getParameter("userId");
            String region = request.getParameter("region");
            int num = 4;
//            Set<String> onLineUsers = GSServices.newFriendManager.getOnLineUsers(num);
//            if (onLineUsers.contains(userId)) {
//                Set<String> strings = GSServices.redisManager.setGetAll(RedisConstant.ONLINE_USER, String.class);
//                if (strings.size() > num) {
//                    for (String s : strings) {
//                        if (!onLineUsers.contains(s)) {
//                            onLineUsers.add(s);
//                            break;
//                        }
//                    }
//                }
//                onLineUsers.remove(userId);
//            }
            Set<String> regionUsers = GSServices.newFriendManager.getRegionUsers(region, num);
            if (regionUsers.contains(userId)) {
                regionUsers.remove(userId);
            }else {
                //多了一个 处理掉
                Iterator<String> iterator = regionUsers.iterator();
                while (iterator.hasNext()) {
                    iterator.next();
                    iterator.remove();
                    break;
                }
            }
            GSServices.localManager.sendMessage(response, true, null, "");
        } catch (Exception e) {
            e.printStackTrace();
            GSServices.localManager.sendMessage(response, false, LocalErrorResponseCode.SYSTEM_ERROR, "");
        }
    }

}
